package com.example.lenovo.weiyi;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.com.ruanmeng.demon.ServiceErWeiMaM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;


/**
 * 服务使用--二维码
 */
public class ServiceUseActivity extends BaseActivity {

    @BindView(R.id.imv_use_erweima)
    ImageView imvUseErweima;
    @BindView(R.id.tv_use_name)
    TextView tvUseName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_use);
        ButterKnife.bind(this);
        ChangLayTitle("服务使用");
        LayBack();
        getData();
    }

    /**
     * 获取我的服务列表
     */
    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ServiceErWeiMa, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ServiceUseActivity.this, "uid"));
        mRequest.add("userProductId", getIntent().getStringExtra("userProductId"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(ServiceUseActivity.this, 0, mRequest,
                new CustomHttpListener(ServiceUseActivity.this, true, ServiceErWeiMaM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        ServiceErWeiMaM model = ((ServiceErWeiMaM) data);
                        if (!TextUtils.isEmpty(model.getQrcode())) {
                            onDecodeClicked(model.getQrcode(), imvUseErweima);
                        }
                        tvUseName.setText(model.getProductName());
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ServiceUseActivity.this, obj.getString("msg"));
                        }

                    }
                }, true, true);
    }
    /**
     * 把base64转成图
     */
    public void onDecodeClicked(String str, ImageView imv) {
        byte[] decode = Base64.decode(str, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        //save to image on sdcard
        saveBitmap(bitmap, imv);
    }

    private void saveBitmap(Bitmap bitmap, ImageView imv) {
        try {
            String path = Environment.getExternalStorageDirectory().getPath()
                    + "/decodeImage.jpg";
            Log.d("linc", "path is " + path);
            OutputStream stream = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
            stream.close();
            imv.setImageBitmap(bitmap);
            Log.e("linc", "jpg okay!");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("linc", "failed: " + e.getMessage());
        }
    }
}
